﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BoAddonBiz_FreightForwarder.Cost.Bill_Draft
{
    class CreateBillDraft
    {
        private BillDraft _Draft;
        public SAPbobsCOM.Company SBOCompany { get; set; }
        public BillDraft Draft
        {
            set { _Draft = value; }
        }
        public BOResult HandleDraft()
        {
            SAPbobsCOM.Documents oDraft = null;
            try
            {
                BOResult result = new BOResult();
                if (SBOCompany.InTransaction == false) SBOCompany.StartTransaction();
                result.Value = false;
                result.Message = "";
                oDraft = SBOCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDrafts);
                if (_Draft.IsModify)
                {
                    if (oDraft.GetByKey(_Draft.DraftKey))
                    {
                        oDraft.Remove();
                    }
                    else
                    {
                        result.Value = false;
                        result.Message = SBOCompany.GetLastErrorCode().ToString() + "  " + SBOCompany.GetLastErrorDescription();
                        if (SBOCompany.InTransaction) SBOCompany.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_RollBack);
                        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oDraft);
                        return result;
                    }
                }
                if (_Draft.IsModify)
                    oDraft = SBOCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDrafts);

                if (_Draft.OrderType == Bo.OrderType.ARInvoice)
                    oDraft.DocObjectCode = SAPbobsCOM.BoObjectTypes.oInvoices;
                if (_Draft.OrderType == Bo.OrderType.APInvoice)
                    oDraft.DocObjectCode = SAPbobsCOM.BoObjectTypes.oPurchaseInvoices;
                oDraft.CardCode = _Draft.CardCode;
                oDraft.DocDate = _Draft.DocDate;
                oDraft.DocCurrency = _Draft.Currency;
                oDraft.DocRate = _Draft.Rate;
                foreach (BillDraftLine line in _Draft.Lines)
                {
                    oDraft.Lines.SetCurrentLine(oDraft.Lines.Count - 1);
                    oDraft.Lines.BaseType = line.BaseType;
                    oDraft.Lines.BaseEntry = line.BaseEntry;
                    oDraft.Lines.BaseLine = line.BaseLine;
                    oDraft.Lines.Currency = line.Currency;
                    oDraft.Lines.Rate = line.Rate;
                    oDraft.Lines.Add();
                }
                if (_Draft.Lines.Count <= 0)
                {
                    result.Value = true;
                    if (SBOCompany.InTransaction) SBOCompany.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_Commit);
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oDraft);
                    return result;
                }
                //oDraft.SaveToFile(@"C:\Documents and Settings\Administrator\桌面\addtest.xml");
                var DIResult = oDraft.Add();
                if (DIResult != 0)
                {
                    result.Value = false;
                    result.Message = SBOCompany.GetLastErrorCode().ToString() + "  " + SBOCompany.GetLastErrorDescription();
                    if (SBOCompany.InTransaction) SBOCompany.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_RollBack);
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oDraft);
                    return result;
                }
                else
                {
                    result.Value = true;
                    result.Message = "已生成草稿凭证 No. - " + SBOCompany.GetNewObjectKey();
                    if (SBOCompany.InTransaction) SBOCompany.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_Commit);
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oDraft);
                    return result;
                }
            }
            catch (Exception ex)
            {
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oDraft);
                throw (ex);
            }
        }
    }
    internal class BOResult
    {
        public bool Value { get; set; }
        public string Message { get; set; }
    }
}
